//
//  BKHeZhaoViewModel.m
//  baokaodaxue
//
//  Created by dangyangyang on 15/6/4.
//  Copyright (c) 2015年 宏图教育. All rights reserved.
//

#import "BKHeZhaoViewModel.h"
#import <AFNetworking.h>
@implementation BKHeZhaoViewModel


+(void)cityOfProv:(NSString *)cid success:(void (^)(NSArray *))success failure:(void (^)(NSString *))failure{

    NSDictionary *params = @{@"provid":cid};
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
    [manager GET:@"http://www.baokaodaxue.com/bk/index/getcity"
      parameters:params
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSDictionary *responseDic=[NSJSONSerialization
                                        JSONObjectWithData:responseObject
                                        options:NSJSONReadingMutableLeaves
                                        error:nil];
             if ([[responseDic objectForKey:@"code"] isEqualToString:@"200"]) {
                 
                 NSArray *infoArray = [responseDic objectForKey:@"extraData"];
                     success(infoArray);
                 
             }else{
                 failure([responseDic objectForKey:@"message"]);
                 
             }
             
         }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             failure(@"网络连接失败");
         }];
    

    
}

+ (void)countryOfCity:(NSString *)cityid success:(void (^)(NSArray *))success failure:(void (^)(NSString *))failure{

    NSDictionary *params = @{@"cityid":cityid};
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
    [manager GET:@"http://www.baokaodaxue.com/bk/index/getcounty"
      parameters:params
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSDictionary *responseDic=[NSJSONSerialization
                                        JSONObjectWithData:responseObject
                                        options:NSJSONReadingMutableLeaves
                                        error:nil];
             if ([[responseDic objectForKey:@"code"] isEqualToString:@"200"]) {
                 
                 NSArray *infoArray = [responseDic objectForKey:@"extraData"];
                 success(infoArray);
                 
             }else{
                 failure([responseDic objectForKey:@"message"]);
                 
             }
             
         }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             failure(@"网络连接失败");
         }];

    
}

+ (void)banjiWithParams:(NSDictionary *)params success:(void (^)(NSArray *))success failure:(void (^)(NSString *))failure{

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
    [manager GET:@"http://www.baokaodaxue.com/bk/mobile/sbanji"
      parameters:params
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSDictionary *responseDic=[NSJSONSerialization
                                        JSONObjectWithData:responseObject
                                        options:NSJSONReadingMutableLeaves
                                        error:nil];
             if ([[responseDic objectForKey:@"code"] isEqualToString:@"200"]) {
                 
                 NSArray *infoArray = [responseDic objectForKey:@"extraData"];
                 success(infoArray);
                 
             }else{
                 failure([responseDic objectForKey:@"message"]);
                 
             }
             
         }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             failure(@"网络连接失败");
         }];

}

+ (void)chuangjianWithParams:(NSDictionary *)params success:(void (^)(NSString *))success failure:(void (^)(NSString *))failure{

    NSURL *url = [NSURL URLWithString:@"http://www.baokaodaxue.com/bk/mobile/iosschuangjian"];
    
    NSString *post = [NSString stringWithFormat:@"provid=%@&cityid=%@&countyid=%@&year=%@&xuexiao=%@&ban=%@&pass=%@&xuehao=%@&sid=%@", params[@"provid"],params[@"cityid"],params[@"countyid"],params[@"year"],params[@"xuexiao"],params[@"ban"],params[@"pass"],params[@"xuehao"],params[@"sid"]];
    
    
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:postData];
    [request setTimeoutInterval:10.0];
    
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    
    
    NSDictionary *cookieDic = [userDefault objectForKey:@"Cookie"];
    NSString *cookieString = [NSString stringWithFormat:@"%@=%@",cookieDic[@"name"],cookieDic[@"value"]];
    
    [request setValue:cookieString forHTTPHeaderField:@"Cookie"];
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    
    
    
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:queue
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
                               if (error) {
                                   NSLog(@"Httperror:%@%ld", error.localizedDescription,(long)error.code);
                               }else{
                                   NSDictionary *responseDic=[NSJSONSerialization JSONObjectWithData:data
                                                                                             options:NSJSONReadingMutableLeaves
                                                                                               error:nil];
                                   
                                   if ([[responseDic objectForKey:@"code"] isEqualToString:@"200"]) {
                                       
                                       success(responseDic[@"extraData"]);
                                       
                                   }else{
                                       
                                       NSString *messge = [responseDic objectForKey:@"message"];
                                       if ([messge isEqualToString:@"请先登录"]) {
                                           NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                                           [userDefaults removeObjectForKey:@"Cookie"];
                                           
                                       }
                                       failure([responseDic objectForKey:@"message"]);
                                   }
                               }
                           }];
    
    

}

+ (void)tijaoWithParams:(NSDictionary *)params success:(void (^)())success failure:(void (^)(NSString *))failure{
    NSURL *url = [NSURL URLWithString:@"http://www.baokaodaxue.com/bk/mobile/ioscjren"];
    
    NSString *post = [NSString stringWithFormat:@"id=%@&xuehao=%@&name=%@&liuyan=%@&imgstr=%@&p=%@", params[@"id"],params[@"xuehao"],params[@"name"],params[@"liuyan"],params[@"imgstr"],params[@"p"]];
    
    
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:postData];
    [request setTimeoutInterval:10.0];
    
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    
    
    NSDictionary *cookieDic = [userDefault objectForKey:@"Cookie"];
    NSString *cookieString = [NSString stringWithFormat:@"%@=%@",cookieDic[@"name"],cookieDic[@"value"]];
    
    [request setValue:cookieString forHTTPHeaderField:@"Cookie"];
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    
    
    
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:queue
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
                               if (error) {
                                  failure(@"网络超时");
                               }else{
                                   NSDictionary *responseDic=[NSJSONSerialization JSONObjectWithData:data
                                                                                             options:NSJSONReadingMutableLeaves
                                                                                               error:nil];
                                   
                                   if ([[responseDic objectForKey:@"code"] isEqualToString:@"200"]) {
                                       
                                       success();
                                       
                                   }else{
                                       
                                       NSString *messge = [responseDic objectForKey:@"message"];
                                       if ([messge isEqualToString:@"请先登录"]) {
                                           NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                                           [userDefaults removeObjectForKey:@"Cookie"];
                                           
                                       }
                                       failure([responseDic objectForKey:@"message"]);
                                   }
                               }
                           }];
    


}

+ (void)shengqingWithParams:(NSDictionary *)params success:(void (^)())success failure:(void (^)(NSString *))failure{

    NSURL *url = [NSURL URLWithString:@"http://www.baokaodaxue.com/bk/mobile/iosjiaru"];
    
    NSString *post = [NSString stringWithFormat:@"id=%@&xuehao=%@&name=%@&liuyan=%@&imgstr=%@&p=%@", params[@"id"],params[@"xuehao"],params[@"name"],params[@"liuyan"],params[@"imgstr"],params[@"p"]];
    
    
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:postData];
    [request setTimeoutInterval:10.0];
    
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    
    
    NSDictionary *cookieDic = [userDefault objectForKey:@"Cookie"];
    NSString *cookieString = [NSString stringWithFormat:@"%@=%@",cookieDic[@"name"],cookieDic[@"value"]];
    
    [request setValue:cookieString forHTTPHeaderField:@"Cookie"];
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    
    
    
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:queue
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
                               if (error) {
                                   NSLog(@"Httperror:%@%ld", error.localizedDescription,(long)error.code);
                               }else{
                                   NSDictionary *responseDic=[NSJSONSerialization JSONObjectWithData:data
                                                                                             options:NSJSONReadingMutableLeaves
                                                                                               error:nil];
                                   
                                   if ([[responseDic objectForKey:@"code"] isEqualToString:@"200"]) {
                                       
                                       success(responseDic[@"extraData"]);
                                       
                                   }else{
                                       
                                       NSString *messge = [responseDic objectForKey:@"message"];
                                       if ([messge isEqualToString:@"请先登录"]) {
                                           NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                                           [userDefaults removeObjectForKey:@"Cookie"];
                                           
                                       }
                                       failure([responseDic objectForKey:@"message"]);
                                   }
                               }
                           }];
    

}

+ (void)zhongxueWithXian:(NSString *)xian success:(void (^)(NSArray *))success failure:(void (^)(NSString *))failure{

    NSDictionary *params = @{@"countyid":xian,
                             @"page":@"1",
                             @"len":@"100000"};
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
    [manager GET:@"http://www.baokaodaxue.com/bk/mobile/szhongxue"
      parameters:params
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSDictionary *responseDic=[NSJSONSerialization
                                        JSONObjectWithData:responseObject
                                        options:NSJSONReadingMutableLeaves
                                        error:nil];
             if ([[responseDic objectForKey:@"code"] isEqualToString:@"200"]) {
                 
                 NSArray *infoArray = [responseDic objectForKey:@"extraData"];
                 success(infoArray);
                 
             }else{
                 failure([responseDic objectForKey:@"message"]);
                 
             }
             
         }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             failure(@"网络连接失败");
         }];
    

    
}

+ (void)hezhaoWithbanji:(NSString *)banji success:(void (^)(NSArray *))success failure:(void (^)(NSString *))failure{

    NSDictionary *params = @{@"id":banji};
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
    [manager GET:@"http://www.baokaodaxue.com/bk/mobile/iosgetphotos"
      parameters:params
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSDictionary *responseDic=[NSJSONSerialization
                                        JSONObjectWithData:responseObject
                                        options:NSJSONReadingMutableLeaves
                                        error:nil];
             if ([[responseDic objectForKey:@"code"] isEqualToString:@"200"]) {
                 
                 NSArray *infoArray = [responseDic objectForKey:@"extraData"];
                 success(infoArray);
                 
             }else{
                 failure([responseDic objectForKey:@"message"]);
                 
             }
             
         }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             failure(@"网络连接失败");
         }];
    
    

    
}
@end
